import {list} from '../api/menu.ts'
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import {computed, ref} from 'vue'
import {useRouteStore} from "../stores/route"
import NProgress from '../config/progress.ts'
import {useUserStore} from '../stores/user.ts'

const staticRoute = [
    {
      path: '/',
      name: 'home',
	  redirect: '/login',
      component: HomeView,
    },
	// { path: '/:pathMatch(.*)', 
	//   name:'NotFound',
	//   redirect:'/index',
	//   component: () => import('../components/NotFound.vue') 
	// },
	{
	  path: '/login',
	  name: 'Login',
	  component: () => import('../views/Login.vue'),
	},
	
    /*{
      path: '/index',
      name: 'index',
      component: () => HomeView,
	  children:[
		  {
			  path: '/index',
			  name: 'mainpage',
			  meta: { title: '首页', icon: 'HomeFilled'},
			  component: () => import('../views/Index.vue'),
		  }
	  ]
    },*/
	{
	  path: '/user',
	  name: 'PersonalSetting',
	  redirect: '/user/personSetting',
	  component: HomeView,
	  children: [
		  {
			  path:'/assignUserList/:roleId',
			  name: 'AssignUserList',
			  meta: { title: '分配用户', icon: 'User'},
			  component: ()=> import('@/views/system/role/AssignUserList.vue')
		  },
		  {
			  path: '/personSetting',
			  name: 'PersonalSettings',
			  meta: { title: '个人设置', icon: 'Basketball'},
			  component: ()=> import('@/views/system/user/personSetting.vue')
		  },
		  {
			  path: '/index',
			  name: 'mainpage',
			  meta: { title: '首页', icon: 'HomeFilled'},
			  component: () => import('../views/Index.vue'),
		  }
	  ]
	},
  ]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: staticRoute,
})
let count=0
router.beforeEach(async(to, from, next) => {
	console.log("来源："+from.path+",去哪："+to.path)
	NProgress.start();
	//如果是访问登录页，直接放行
	if(to.path==='/login')return next()
	const userStore = useUserStore();
	const token = userStore.token
	const expiredTime = userStore.expireTime
	const now = new Date().getTime();
	//过期的话.过期或者没有的话重定向到login
	if(expiredTime < now || token === ''){
		userStore.clearToken()
	    return next({path:`/login?redirect=${to.path}`,replace:true})
	}
    count++
	if(count == 1){
		 await userStore.setMenus()
		 next(to.fullPath)
		 return
	}
	 next()
});
router.afterEach((to, from, next) => {
  NProgress.done()
  const useRoute = useRouteStore();
  	 const menues = useRoute.routes;
  	 const parentTitle = ref()
  	 const parentRedirect = ref()
  	 menues.forEach((item: { path: string; meta: { title: any }; redirect: any })=>{
  	 	if(to.path.includes(item.path)){
  	 		parentTitle.value = item.meta.title
  	 		parentRedirect.value = item.redirect
  	 	}
  	 })
	 if(to.path != '/index'){
		 let array = [];
		 if(parentRedirect.value != undefined){
			  array.push({title:parentTitle.value,redirect:parentRedirect})
		 }
		 array.push({title:to.meta.title})
		 useRoute.setBreadcrumb(array);
	 }else{
		  useRoute.setBreadcrumb([]);
	 }
	 if(to.path != '/' && to.path != '/login'){
		 let close = true;
		 if (to.meta.title == '首页'){
			 close = false
		 }
		 //判断tab里是否有首页，没有的话添加
		 const editableTabs = computed(() =>useRoute.tabs)
		 const hasMain = editableTabs.value.some(item=>item.name == '首页')
		 if(!hasMain){
			 useRoute.setTab({title: '首页',name: '首页',icon:'HomeFilled',path:'/index',close:false})
		 }
		 useRoute.setTab({title: to.meta.title,name: to.meta.title,icon:to.meta.icon,path:to.path,close:close})

	 }
});
router.onError((to, from, next)=>{
	console.log("to3:"+to.path)
})

export default router
